package com.hitqz.robot.driver.zmq.protocol.handler;

import com.example.tutorial.AddressBookProtos;
import com.hitqz.robot.driver.util.WheelCacheUtil;
import com.hitqz.robot.driver.util.WheelToolkit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 文件操作消息处理类
 *
 * @author wengyx
 * @date 2021-09-14 17:02
 **/
@Slf4j
@Component
public class FileOperatorHandler implements HandlerZMQService {

    @Override
    public void handlerMsg(String robotSn, byte[] data) throws Exception {
        AddressBookProtos.FileOperator fileOperator = AddressBookProtos.FileOperator.parseFrom(data);
        String type = fileOperator.getDescriptorForType().getName();
        int objId = fileOperator.getHeader().getObj();
        int ackId = fileOperator.getHeader().getAck();
        String checkSum = fileOperator.getCheckSum();
        //.tar.gz文件流
        byte[] fileData = fileOperator.getData().toByteArray();
        switch (objId) {
            //返回当前navconnector程序

            //返回运行日志
            case AddressBookProtos.FileOperator.OBJ_ID.GET_RUNNING_LOG_REP_VALUE->{
                WheelCacheUtil.put(WheelCacheUtil.KEY_RUNNING_LOG,fileData);
            }
            //2D地图
            case AddressBookProtos.FileOperator.OBJ_ID.GET_MAP_2D_REP_VALUE->{
                WheelCacheUtil.put(WheelCacheUtil.KEY_MAP_2D,fileData);
            }
            //3D地图
            case AddressBookProtos.FileOperator.OBJ_ID.GET_MAP_3D_REP_VALUE->{
                WheelCacheUtil.put(WheelCacheUtil.KEY_MAP_3D,fileData);
            }
            //PB地图
            case AddressBookProtos.FileOperator.OBJ_ID.GET_MAP_PB_REP_VALUE->{
                WheelCacheUtil.put(WheelCacheUtil.KEY_MAP_PB,fileData);
            }
        }
        //释放锁
        WheelToolkit.release(ackId,type,objId,true);
    }

}
